<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

//setup the layout
/**
 * Rewix extension for Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.rewix.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to contact@rewix.com so we can send you a copy immediately.
 *
 * @category   Rx
 * @package    Rx_Controller_Plugin
 * @copyright  Copyright (c) 2007 Rewix (http://www.rewix.com)
 * @license    http://www.rewix.com/license/new-bsd     New BSD License
 */

require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Rx_Controller_Plugin_LayoutFromRoute
 * It's a plugin for set the layout from the module name
 *
 * @category   Rx
 * @package    Rx_Controller_Plugin
 * @copyright  Copyright (c) 2007 Rewix (http://www.rewix.com)
 * @license    http://www.rewix.com/license/new-bsd     New BSD License
 */
class Module_LayoutPlugin extends Zend_Controller_Plugin_Abstract {
 
	/**
	 * Array of layout paths associating modules with layouts
	 */
	protected $_moduleLayouts;
 
	/**
	 * Registers a module layout.
	 * This layout will be rendered when the specified module is called.
	 * If there is no layout registered for the current module, the default layout as specified
	 * in Zend_Layout will be rendered
	 * 
	 * @param String $module		The name of the module
	 * @param String $layoutPath	The path to the layout
	 * @param String $layout		The name of the layout to render
	 */
	public function registerModuleLayout($module, $layoutPath, $layout=null){
		$this->_moduleLayouts[$module] = array(
			'layoutPath' => $layoutPath,
			'layout' => $layout
		);
	}
 
	public function preDispatch(Zend_Controller_Request_Abstract $request){
		if(isset($this->_moduleLayouts[$request->getModuleName()])){
			$config = $this->_moduleLayouts[$request->getModuleName()];
 
			$layout = Zend_Layout::getMvcInstance();
			if($layout->getMvcEnabled()){
				$layout->setLayoutPath($config['layoutPath']);
 
				if($config['layout'] !== null){
					$layout->setLayout($config['layout']);
				}
			}
		}
	}
}
$frontController = Zend_Controller_Front::getInstance();

Zend_Layout::startMvc(array(
    'layoutPath' => '../application/modules/default/layouts/scritps',
    'layout' => 'layout'
));
 $layoutModulePlugin = new Module_LayoutPlugin();

$layoutModulePlugin->registerModuleLayout('admin','../application/modules/admin/layouts/scripts');
$layoutModulePlugin->registerModuleLayout('default','../application/modules/default/layouts/scripts');
$frontController->registerPlugin($layoutModulePlugin);

$application->bootstrap()
            ->run();


